using System;
using System.Collections.Generic;
using System.Text;

using NBear.Common;
using NBear.IoC.Service;
using NBear.Data;
using SimpleGuestbook.Entities;
using SimpleGuestbook.ServiceInterfaces;
using _Entity = SimpleGuestbook.Entities._Entity;

namespace SimpleGuestbook.ServiceImpls
{
    public class MessageService : IMessageService
    {
        #region IMessageService Members

        public SimpleGuestbook.Entities.Message[] GetMessages(int pageSize, int pageNo, bool includePrivate)
        {
            if (includePrivate)
            {
                return Gateway.Default.SelectAll<Message>(_Entity.Message.PostTime.Desc, pageSize, pageNo);
            }
            else
            {
                return Gateway.Default.Select<Message>(pageSize, pageNo, _Entity.Message.IsPrivate == false, _Entity.Message.PostTime.Desc);
            }
        }

        public void AddMessage(SimpleGuestbook.Entities.Message msg)
        {
            Gateway.Default.Insert<Message>(msg);
        }

        public void UpdateAdminFeedback(int msgID, string feedbackContent)
        {
            string[] columns = new string[] { Gateway.Default.BuildDbColumnName("AdminFeedback"), Gateway.Default.BuildDbColumnName("AdminFeedbackTime") };
            Gateway.Default.Update<Message>(columns, new object[] { feedbackContent, DateTime.Now }, msgID);
        }

        public void DeleteMessage(int id)
        {
            Gateway.Default.Delete<Message>(id);
        }

        public int GetMessagePageCount(int pageSize, bool includePrivate)
        {
            int rowCount;
            if (includePrivate)
            {
                rowCount = Gateway.Default.SelectRowCount<Message>(null, null);
            }
            else
            {
                rowCount = Gateway.Default.SelectRowCount<Message>(_Entity.Message.IsPrivate == false);
            }
            if (rowCount % pageSize == 0)
            {
                return Convert.ToInt32(rowCount / pageSize);
            }
            else
            {
                return Convert.ToInt32(rowCount / pageSize) + 1;
            }
        }

        #endregion
    }

    public class AdminService : IAdminService
    {
        #region IAdminService Members

        public bool VerifyPassword(string password)
        {
            return Gateway.Default.Exists<Admin>(_Entity.Admin.Password == new CryptographyHelper().ComputeHash(password));
        }

        public void ResetPassword(string password)
        {
            Admin obj = EntityFactory<Admin>.CreateObject();
            obj.Password = new CryptographyHelper().ComputeHash(password);

            if (Gateway.Default.SelectRowCount<Admin>(null, null) > 0)
            {
                Gateway.Default.Update<Admin>(new string[] { Gateway.Default.BuildDbColumnName("Password") }, new object[] { obj.Password });
            }
            else
            {
                Gateway.Default.Insert<Admin>(obj);
            }
        }

        #endregion
    }
}
